// 编辑状态设置为禁用
export const mutiSetDisabled = (propertyType, currentRocord) => {
  // 如果已经创建xbom对象，则都不可编辑
  if (currentRocord.xbomId) {
    propertyType.plantId = {
      ...propertyType.plantId,
      props: {
        ...propertyType.plantId?.props,
        disabled: true,
      },
    };
    // 生效时间只有在研发审批结束后才不允许修改
    propertyType.effectiveTimeStr = {
      ...propertyType.effectiveTimeStr,
      props: {
        ...propertyType.effectiveTimeStr?.props,
        disabled: true,
      },
    };
  }
  return propertyType;
};

// 按钮操作项
export const btnsEnum = Object.freeze({
  createXbom: 0,
  previewXbomSettlement: 1,
  previewBaseSettlement: 2,
  previewColorsSettlement: 3,
  createXSet: 4,
  createResearchApproval: 5,
  createWorkApproval: 6,
  issued: 7,
  dmsEntry: 8,
  sapEntry: 9,
});

// xbom对象状态
export const stateEnum = Object.freeze({
  notReceived: 1, // 未接收
  received: 2, // 已接收
  maintained: 3, // 已维护
  researchApprovalRejected: 4, // 研发驳回
  researchApprovalIng: 5, // 研发审批中
  researchApprovalEnd: 6, // 研发审批结束
  processApprovalRejected: 7, // 工艺驳回
  processApprovalIng: 8, // 工艺审批中
  processApprovalEnd: 9, // 工艺审批结束
  issuedEnd: 10, // 已下发
  dmsFail: 11, // 下发dms失败
  dmsSuccess: 12, // 下发dms成功（同13，后端单独为下发dms重试成功做区分）
  sapFail: 13, // 下发sap失败
});

// 判断下发时间是否可以编辑
export const audgeEffectiveTimeStrIsCanEdit = (xbomObject) => {
  const state = +xbomObject?.state;
  return [stateEnum.notReceived, stateEnum.received, btnsEnum.maintained, btnsEnum.researchApprovalRejected].includes(
    state,
  );
};

// 根据xbom对象状态来判断按钮的操作权限 --- trye表示可以操作
export const audgeAuthFromState = (btnKey, xbomObject = {}) => {
  const state = +xbomObject?.state;
  const hasCreateXbom = !!xbomObject?.xbomNo; // 是否已经创建过xbom对象
  switch (state) {
    // 未接收时候
    case stateEnum.notReceived:
      return false;
      break;
    // 已接收时候
    case stateEnum.received:
      if (hasCreateXbom) {
        return [
          btnsEnum.createXbom,
          btnsEnum.createXSet,
          btnsEnum.previewXbomSettlement,
          btnsEnum.previewBaseSettlement,
          btnsEnum.createResearchApproval,
        ].includes(btnKey);
      } else {
        // 未创建xbom则不可以创建x配置
        return [btnsEnum.createXbom].includes(btnKey);
      }
      break;
    // 已维护时候
    case stateEnum.maintained:
      return [
        btnsEnum.createXbom,
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewBaseSettlement,
        btnsEnum.createResearchApproval,
      ].includes(btnKey);
      break;
    // 研发审批驳回
    case stateEnum.researchApprovalRejected:
      return [
        btnsEnum.createXbom,
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewBaseSettlement,
        btnsEnum.createResearchApproval,
      ].includes(btnKey);
      break;
    // 研发审批中
    case stateEnum.researchApprovalIng:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewBaseSettlement,
        btnsEnum.createResearchApproval,
      ].includes(btnKey);
      break;
    // 已研发审批
    case stateEnum.researchApprovalEnd:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewColorsSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
      ].includes(btnKey);
      break;
    // 工艺审批驳回
    case stateEnum.processApprovalRejected:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewColorsSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
      ].includes(btnKey);
      break;
    // 工艺审批中
    case stateEnum.processApprovalIng:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.previewColorsSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
      ].includes(btnKey);
      break;
    // 已工艺审批
    case stateEnum.processApprovalEnd:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
        btnsEnum.issued,
      ].includes(btnKey);
      break;
    // 已下发
    case stateEnum.issuedEnd:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
      ].includes(btnKey);
    // 下发dms和维护都失败
    case stateEnum.dmsFail:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
        btnsEnum.dmsEntry,
      ].includes(btnKey);
    // 下发dms成功，下发sap失败
    case stateEnum.dmsSuccess:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
        btnsEnum.sapEntry,
      ].includes(btnKey);
    // 下发sap失败，下发dms成功
    case stateEnum.sapFail:
      return [
        btnsEnum.createXSet,
        btnsEnum.previewXbomSettlement,
        btnsEnum.createResearchApproval,
        btnsEnum.createWorkApproval,
        btnsEnum.sapEntry,
      ].includes(btnKey);
      break;
  }
};

// 是否可以进行订单复制 -- 创建xbom后才可以,研发审批结束后不可以
export const canOrderCopy = (tableData = {}) => {
  const state = +tableData?.state;
  return [stateEnum.received, stateEnum.maintained, stateEnum.researchApprovalRejected].includes(state);
};

// 数组对象去重 - 根据plantId
export const uniqueArr = (arr = [], filed = 'value') => {
  return [...new Map(arr.map((item) => [item[filed], item])).values()];
};

// 下发失败的重试状态 - 管理员开后门可以修改x配置列表和bom维护列表
export const entrySendDownStatus = [stateEnum.dmsFail, stateEnum.dmsSuccess, stateEnum.sapFail];
